using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace CSharpPro._0930
{
    public class User
    {
        private static Dictionary<string, string> _userDict = new Dictionary<string, string>();
        public static void ShowMainMenu()
        {
            while (true)
            {
                Console.WriteLine("\n===== 主菜单 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请选择功能（1-3）：");

                string input = Console.ReadLine().Trim();
                if (string.IsNullOrEmpty(input))
                {
                    Console.WriteLine("输入不可为空，请重新输入！");
                    continue;
                }

                if (!int.TryParse(input, out int choice))
                {
                    Console.WriteLine("请输入有效数字！");
                    continue;
                }

                switch (choice)
                {
                    case 1:
                        Login();
                        break;
                    case 2:
                        Register();
                        break;
                    case 3:
                        Console.WriteLine("感谢使用，再见！");
                        return;
                    default:
                        Console.WriteLine("选择无效，请重新输入！");
                        break;
                }
            }
        }
        public static void Register()
        {
            Console.WriteLine("\n===== 用户注册 =====");
            string username;
            while (true)
            {
                Console.Write("请输入用户名（3-15位，仅含字母、数字、下划线）：");
                username = Console.ReadLine().Trim();
                if (string.IsNullOrEmpty(username))
                {
                    Console.WriteLine("用户名不可为空！");
                    continue;
                }
                if (username.Length < 3 || username.Length > 15)
                {
                    Console.WriteLine("用户名长度必须为3-15个字符！");
                    continue;
                }
                if (!Regex.IsMatch(username, @"^[a-zA-Z0-9_]+$"))
                {
                    Console.WriteLine("用户名只能包含字母、数字和下划线！");
                    continue;
                }
                if (_userDict.ContainsKey(username))
                {
                    Console.WriteLine("用户名已存在，请重新输入！");
                    continue;
                }
                break;
            }
            string password;
            while (true)
            {
                Console.Write("请输入密码（6-20位，含至少1个大写、1个小写、1个数字）：");
                password = Console.ReadLine().Trim();
                if (string.IsNullOrEmpty(password))
                {
                    Console.WriteLine("密码不可为空！");
                    continue;
                }
                if (password.Length < 6 || password.Length > 20)
                {
                    Console.WriteLine("密码长度必须为6-20个字符！");
                    continue;
                }
                if (!Regex.IsMatch(password, @"^(?=.*[A-Z])(?=.*[a-z])(?=.*\d).+$"))
                {
                    Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字！");
                    continue;
                }

                Console.Write("请确认密码：");
                string confirmPwd = Console.ReadLine().Trim();
                if (password != confirmPwd)
                {
                    Console.WriteLine("两次密码输入不一致，请重新输入！");
                    continue;
                }
                break;
            }
            _userDict.Add(username, password);
            Console.WriteLine("注册成功！已自动返回主菜单。");
        }
        public static void Login()
        {
            Console.WriteLine("\n===== 用户登录 =====");

            Console.Write("请输入用户名：");
            string username = Console.ReadLine().Trim();
            if (string.IsNullOrEmpty(username))
            {
                Console.WriteLine("用户名不可为空！");
                return;
            }

            Console.Write("请输入密码：");
            string password = Console.ReadLine().Trim();
            if (string.IsNullOrEmpty(password))
            {
                Console.WriteLine("密码不可为空！");
                return;
            }

            if (_userDict.TryGetValue(username, out string storedPwd) && storedPwd == password)
            {
                Console.WriteLine("登录成功！进入系统首页（模拟）。");
            }
            else
            {
                if (!_userDict.ContainsKey(username))
                    Console.WriteLine("用户名不存在！");
                else
                    Console.WriteLine("密码错误！");
            }
        }
    }
}